﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using System.Data;
using BLL;

public partial class MembershipCard_wechat_web_AddressEdit2 : WeiPage
{
    PRODUCTBO pbo = new PRODUCTBO();
    ShoppingCartBO sbo = new ShoppingCartBO();
    PersonInfoBO fbo = new PersonInfoBO();
    AddressBO abo = new AddressBO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["code"] != null)
            {
                try
                {
                    string url = string.Format(@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + GetAppid() + "&secret=" + GetSecret() + "&code={0}&grant_type=authorization_code", Request.QueryString["code"].ToString());
                    string token = PostRequest(url);
                    if (token.Contains("7200"))
                    {
                        string[] b = token.Split('\"');
                        Session["FromUserName"] = b[13];
                        Session["ToUserName"] = GetAppid();
                    }
                }
                catch (Exception)
                {

                }
            }
            if ((Request.QueryString["FromUserName"] != null || Session["FromUserName"] != null) && (Request.QueryString["ToUserName"] != null || Session["ToUserName"] != null))
            {
                DataSet city = fbo.GetCityCode();

                foreach (DataRow dr in city.Tables[0].Rows)
                {
                    qy.Items.Add(new ListItem(dr["name"].ToString(), dr["name"].ToString()));
                }


                string user = Request.QueryString["FromUserName"] == null ? Session["FromUserName"].ToString() : Request.QueryString["FromUserName"].ToString();
                string user2 = Request.QueryString["ToUserName"] == null ? Session["ToUserName"].ToString() : Request.QueryString["ToUserName"].ToString();
                Session["FromUserName"] = user;
                Session["ToUserName"] = user2;
                PersonInfo per = fbo.GetModel(user);
                if (per != null)
                {
                    //List<ShoppingCart> list = sbo.GetModelList(" pid=" + per.ID);
                    //if (list.Count > 0)
                    //{
                    //    sumcar.InnerHtml = "<span><a href='ShopCar2.aspx'><em class='indexicon4'></em><i>" + list.Count + "</i></a></span>";
                    //}

                    if (Request.QueryString["id"] != null)
                    {
                        Addresss a = abo.GetModel(int.Parse(Request.QueryString["id"].ToString()));
                        if (a != null)
                        {
                            xm.Value = a.Name;
                            qy.SelectedValue = a.Sheng;
                            shi.Value = a.Shi;
                            dz.Value = a.Address;
                            post.Value = a.Post;
                            dh.Value = a.Phone;
                        }
                    }
                }


            }

        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if ((Request.QueryString["FromUserName"] != null || Session["FromUserName"] != null) && (Request.QueryString["ToUserName"] != null || Session["ToUserName"] != null))
        {
            string user = Request.QueryString["FromUserName"] == null ? Session["FromUserName"].ToString() : Request.QueryString["FromUserName"].ToString();
            string user2 = Request.QueryString["ToUserName"] == null ? Session["ToUserName"].ToString() : Request.QueryString["ToUserName"].ToString();
            Session["FromUserName"] = user;
            Session["ToUserName"] = user2;
            PersonInfo per = fbo.GetModel(user);
            if (per != null)
            {
                Addresss add = new Addresss();
                if (Request.QueryString["id"] != null)
                {
                    add = abo.GetModel(int.Parse(Request.QueryString["id"].ToString()));
                }
                add.Address = dz.Value;
                add.LAST_DATE = DateTime.Now;
                add.Name = xm.Value;
                add.Phone = dh.Value;
                add.PID = per.ID;
                add.Post = post.Value;
                add.Sheng = qy.SelectedValue;
                add.Shi = shi.Value;
                if (add.Address == "" || add.Name == "" || add.Phone == "")
                {
                    AlertMessage("地址、姓名、电话为必输项");
                    return;
                }
                if (Request.QueryString["id"] != null)
                {
                    abo.Update(add);
                }
                else
                    abo.Add(add);
                if (Request.QueryString["g"] != null)
                    Response.Redirect("address2.aspx?g=" + Request.QueryString["g"].ToString());
                else
                    Response.Redirect("address2.aspx");
            }
        }

    }
}